package com.example.xjtuse_pc.services;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.util.Log;

import com.example.xjtuse_pc.db.dao.BlackNumberDao;

import java.util.Objects;

/**
 * Created by PC on 2016-4-13.
 */
public class BlackNumberSmsService extends Service {
    private InnerSmsReceive receiver;
    private BlackNumberDao dao;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class  InnerSmsReceive extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //检查发件人是否是黑名单号码，设置短信拦截全部拦截。
            Object[] obj= (Object[]) intent.getExtras().get("pdus");
            for(Object o:obj){
                SmsMessage smsMessage=SmsMessage.createFromPdu((byte[]) o);
                String number=smsMessage.getOriginatingAddress();
                String result=dao.findMode(number);
                if(result.equals("2")||result.equals("3")){
                    Log.i("join", "拦截短信");
                    abortBroadcast();
                }

            }

        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        dao = new BlackNumberDao(this);
        receiver=new InnerSmsReceive();
        IntentFilter filter=new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(receiver,filter);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
